package com.bootdo.wx.controller;

import com.bootdo.wx.domain.GoodsBrandDO;
import com.bootdo.wx.service.GoodsBrandService;
import com.bootdo.common.config.Constant;
import com.bootdo.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商品品牌
 * 
 * @author sys
 * @email 1992lcg@163.com
 * @date 2019-03-19 16:39:22
 */
 
@Controller
@RequestMapping("/wx/goodsBrand")
public class GoodsBrandController {
	@Autowired
	private GoodsBrandService goodsBrandService;
	
	@GetMapping()
	@RequiresPermissions("wx:goodsBrand:goodsBrand")
	String GoodsBrand(){
	    return "wx/goodsBrand/goodsBrand";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:goodsBrand:goodsBrand")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
		params.put("delFlag","0");
        Query query = new Query(params);
		List<GoodsBrandDO> goodsBrandList = goodsBrandService.list(query);
		int total = goodsBrandService.count(query);
		PageUtils pageUtils = new PageUtils(goodsBrandList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:goodsBrand:add")
	String add(){
	    return "wx/goodsBrand/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:goodsBrand:edit")
	String edit(@PathVariable("id") String id,Model model){
		GoodsBrandDO goodsBrand = goodsBrandService.get(id);
		model.addAttribute("goodsBrand", goodsBrand);
	    return "wx/goodsBrand/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:goodsBrand:add")
	public R save( GoodsBrandDO goodsBrand){
		goodsBrand.setId(UuidUtil.get32UUID());
		goodsBrand.setUserIdCreate(ShiroUtils.getUserId());
		goodsBrand.setGmtCreate(new Date());
		goodsBrand.setDelFlag(Constant.DEFAULT_DEL_FLAG);
		if(goodsBrandService.save(goodsBrand)>0){
			return R.ok();
		}
		return R.error();
	}
	

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:goodsBrand:edit")
	public R update( GoodsBrandDO goodsBrand){
		goodsBrand.setGmtModified(new Date());
		goodsBrandService.update(goodsBrand);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:goodsBrand:remove")
	public R remove( String id){
		GoodsBrandDO goodsBrandDO = goodsBrandService.get(id);
		goodsBrandDO.setDelFlag(1);
		goodsBrandDO.setGmtModified(new Date());
		if(goodsBrandService.update(goodsBrandDO)>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:goodsBrand:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		for (String id : ids) {
			GoodsBrandDO goodsBrandDO = goodsBrandService.get(id);
			goodsBrandDO.setDelFlag(1);
			goodsBrandDO.setGmtModified(new Date());
			goodsBrandService.update(goodsBrandDO);
		}
		return R.ok();
	}
	
}
